package cn.jbit.supermarker.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.jbit.supermarker.entity.Provider;
import cn.jbit.supermarker.service.ProviderService;
import cn.jbit.supermarker.service.impl.ProviderServiceImpl;
import cn.jbit.supermarker.util.Page;

public class ProviderServlet extends HttpServlet implements Servlet {

	
	public ProviderServlet() {
		super();
	}

	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action=request.getParameter("flag");
		if("fenye".equals(action)){
			this.doPageProvider(request, response);
		}
		
	}
	//分页显示数据
	public void doPageProvider(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String currentPage = request.getParameter("pageIndex");
		if(currentPage==null || "".equals(currentPage)){
			currentPage="1";
		}
		int pageIndex = Integer.parseInt(currentPage);
		if (pageIndex < 1) {
			pageIndex = 1;
		}
		ProviderServiceImpl providerService=new ProviderServiceImpl();
		String proName = request.getParameter("proName");
		int total = providerService.getTotalCount(proName);
		int pageSize = 2;
		Page page = new Page();
		page.setPageSize(pageSize);
		page.setTotalCount(total);
		int totalPage = page.getTotalPageCount();
		if (pageIndex > totalPage) {
			pageIndex = totalPage;
		}
		page.setCurrPageNo(pageIndex);
		List<Provider> proList = providerService.getProPageList(pageIndex,
				pageSize, proName);
		request.setAttribute("proList", proList);
		request.setAttribute("page", page);
		request.setAttribute("proName", proName);
		request.getRequestDispatcher("providerList.jsp").forward(request,response);
	}
	
	public void init() throws ServletException {
		// Put your code here
	}

}
